package cn.yunhe.dao;

import cn.yunhe.entity.News;
import cn.yunhe.util.BaseDB;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDao extends BaseDB implements INewsDao {
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;


    @Override
    public List<News> getAllNews() {
        List<News> newsList = new ArrayList<News>();
        try {
            conn = getConn();
            String sql = "select * from t_news";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                News n = new News(rs.getInt("newsId"), rs.getString("title"), rs.getString("context"), rs.getString("writedate"));
                newsList.add(n);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }


        return newsList;
    }
}
